package TouristManagementBO.TransportSystem;

import TouristManagement.*;

public class TransportSystemServer 
{	
	private ServerNamingTourist leServerNamingTourist;
	
	public void initServer(){
		this.connectToServerNamingTourist();
	}
	
	public void connectToServerNamingTourist(){
		//init VisitManagement server
		try {
			
	        String args[]=null;

			// Intialisation de l'orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Recuperation du naming service
	        org.omg.CosNaming.NamingContext nameRoot =
	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

	        // Construction du nom a rechercher
	        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
	         nameToFind[0] = new org.omg.CosNaming.NameComponent("ServerNamingTourist","");

	        // Recherche aupres du naming service
	        org.omg.CORBA.Object distantServerNamingTourist = nameRoot.resolve(nameToFind);
	        
	        leServerNamingTourist = ServerNamingTouristHelper.narrow(distantServerNamingTourist);
	        
	        System.out.println("Tourist naming server connected.");
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public static void main(String[] args) {
		//launch 5 transport system servers
		
		TransportSystemServer leServer = new TransportSystemServer();
		
		leServer.initServer();
		
		String[] lesSystems = leServer.leServerNamingTourist.nomSystemTransports();
		
		for (String nomSystem:lesSystems) {
			TransportSystemServerThread leSystem = new TransportSystemServerThread(nomSystem);
			System.out.println("Starting system transport "+nomSystem);			
			leSystem.start();
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}		
	} // main
}
